package com.UU.J2EE.PizzaStore.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import com.UU.J2EE.PizzaStore.beans.CustomerBean;


public class CustomerDAO {

	//Register
	public boolean Register(CustomerBean customer)
	{
		boolean b = true;
		String sql = "insert into customer values(?,?,?,?,?,?)";
		String []parameters = {Integer.toString(0),customer.getName(),customer.getPassword(),customer.getEmail(),customer.getPhone(),customer.getAddress()};
		try{
			DBUtil.executeUpdates(sql, parameters);
		}catch (Exception e) {
			b = false;
			System.out.println(e);
			// TODO: handle exception
		}
		return b;
	}
	public static boolean testCustomer(CustomerBean customer) {
        ResultSet rs=null;
        boolean hasOrNot = false;
        try{
            String sql;
            sql ="SELECT * from  customer where name = " +
                     "'" + customer.getName() + "'";
            rs= DBUtil.executeQuery(sql,null);
	    // analyze the result set
	    hasOrNot = rs.next();
	    if(hasOrNot)
	    {
	    	customer.setId(rs.getInt("idcustomer"));
	    	customer.setPassword(rs.getString("password"));
            customer.setEmail(rs.getString("email"));
            customer.setPhone(rs.getString("phone"));
            customer.setAddress(rs.getString("address"));
	    }
	}   
	catch(Exception sqle){
            sqle.printStackTrace();
	}
        finally{
        	DBUtil.close(rs, DBUtil.getPs(), DBUtil.getCt());
        }
        return hasOrNot;
  }
	
	//UpdateInfo
	public static boolean UpdateInfo(CustomerBean customerBean)
	{
		boolean isOK = true;
		String sql = "update customer set password=?, email=?, phone=?, address=? where name=?";
		String parameters[]={customerBean.getPassword(),customerBean.getEmail(),customerBean.getPhone(),customerBean.getAddress(), customerBean.getName()};
		try{
			DBUtil.executeUpdates(sql, parameters);
		}catch (Exception e) {
			isOK = false;
			// TODO: handle exception
		}
		return isOK;
	}
}
